package cn.com.se.业务工具.字体转换工具;

import org.apache.commons.compress.utils.Lists;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FontConverter {
    public static List<String> ttfSql = Lists.newArrayList();
    public static List<String> woffSql = Lists.newArrayList();

    public static void main(String[] args) {
        try {
            int initId = 2774;

            String sourceFolder = "D:\\fastposter\\AddFonts\\新增字体20231211";

            File woffFolder = new File(sourceFolder + "\\WOFF");
            File[] woffFiles = woffFolder.listFiles();

            File ttfFolder = new File(sourceFolder + "\\TTF");
            File[] ttfFiles = ttfFolder.listFiles();

            // 字符串全转小写


            Map<String, File> ttfMap = Arrays.asList(ttfFiles).stream().collect(Collectors.toMap(ttfFile -> {
                return getFileName(ttfFile.getName());
            }, Function.identity(), (k1, k2) -> k1));

            Map<String, File> woffMap = Arrays.asList(woffFiles).stream().collect(Collectors.toMap(woffFile -> {
                return getFileName(woffFile.getName());
            }, Function.identity(), (k1, k2) -> k1));


            for (File file : ttfFiles) {
                String timeStamp = getCurrentTimeStamp();
                String fileName = file.getName();

                File ttfFile = ttfMap.get(getFileName(fileName));
                String newTtfName = timeStamp + getFileExtension(ttfFile.getName());
                createTtf(newTtfName, ttfFile, sourceFolder, initId);

                File woffFile = woffMap.get(getFileName(fileName));
                String newWoffName = timeStamp + getFileExtension(woffFile.getName());
                createWoff(newWoffName, woffFile, sourceFolder, initId);

                initId++;
            }

            ttfSql.forEach(System.out::println);

            woffSql.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void createWoff(String newFileName, File file, String destinationFolder, int initId) {
        int offset = initId;

        if (file.isFile()) {
            String fileName = file.getName();


            try {
                Path sourcePath = file.toPath();

                // 生成woff
                Path woffDestinationPath = new File(destinationFolder + "\\RNAME\\WOFF", newFileName).toPath();
                Files.copy(sourcePath, woffDestinationPath, StandardCopyOption.REPLACE_EXISTING);

                // 生成新增SQL
                woffSql.add(FontConverter.printSql(offset, fileName.split("\\.")[0], newFileName));
            } catch (Exception e) {
                System.out.println("处理异常的文件名: " + fileName);
                e.printStackTrace();
            }
        }

    }


    public static void createTtf(String newFileName, File file, String destinationFolder, int initId) {
        int offset = initId;
        if (file.isFile()) {
            String fileName = file.getName();

            try {
                Path sourcePath = file.toPath();

                // 生成TTF
                Path woffDestinationPath = new File(destinationFolder + "\\RNAME\\TTF", newFileName).toPath();
                Files.copy(sourcePath, woffDestinationPath, StandardCopyOption.REPLACE_EXISTING);

                // 生成新增SQL
                ttfSql.add(FontConverter.printSql(offset, fileName.split("\\.")[0], newFileName));
            } catch (Exception e) {
                System.out.println("处理异常的文件名: " + fileName);
                e.printStackTrace();
            }
        }

    }


    private static String getCurrentTimeStamp() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return now.format(formatter);
    }

    private static String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex);
        }
        return "";
    }

    private static String getFileName(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(0, dotIndex).toLowerCase();
        }
        return "";
    }

    private static String printSql(int id, String fontName, String newFontName) {
        return "INSERT INTO fastposter.fp_fonts (id, uid, name, code, `type`, create_time, update_time, family_name) VALUES(" + id + ", 0, '" + fontName + "', 'sy/" + newFontName + "', 1, NULL, '" + FontConverter.getTime("yyyy-MM-dd hh:mm:ss") + "', '" + fontName + "');";
    }


    private static String getTime(String format) {
        LocalDateTime now = LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return now.format(formatter);
    }


}
